/**
 * 
 */
package com.google.code.jkippt.http;

import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.google.code.jkippt.util.ListBuilder;

/**
 * @author "Federico De Faveri defaveri@gmail.com"
 *
 */
public class ParametersBuilder {
	
	protected Map<String, String> parameters;
	
	protected void checkParametersInitialization()
	{
		if (parameters == null) parameters = new HashMap<String, String>();
	}
	
	public void add(String name, String value)
	{
		if (value == null) return;
		checkParametersInitialization();
		parameters.put(name, value);
	}
	
	public void add(String name, Calendar value)
	{
		if (value == null) return;
		checkParametersInitialization();		
		parameters.put(name, String.valueOf(value.getTimeInMillis()));
	}
	
	public void add(String name, boolean[] values, String[] labels)
	{
		if (values.length != labels.length) throw new IllegalArgumentException("Values and labels should have the same number of elements");
		if (values.length == 0) return;
		if (allFalse(values)) return;
		
		ListBuilder listBuilder = new ListBuilder();
		for (int i = 0; i < values.length; i++) {
			if (values[i]) listBuilder.addElement(labels[i]);
		}
		add(name, listBuilder.build());
	}
	
	protected boolean allFalse(boolean[] values)
	{
		for (boolean value:values) if (value) return false;
		return true;
	}
	
	
	public Map<String, String> build()
	{
		if (parameters == null) return Collections.emptyMap();
		return parameters;
	}
	
	public static boolean[] values(boolean ... values)
	{
		return values;
	}
	

}
